package com.nativetalk.service.three.index;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.banner.TdNtBannerInfoIndex;
import com.nativetalk.bean.dict.TsNtDictDeatil;
import com.nativetalk.bean.member.TdNtMember;
import com.nativetalk.bean.search.TdNtSearchHistory;
import com.nativetalk.bean.teacher.TdNtTeacherList;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.dao.TdNtLiveCourseHisDao;
import com.nativetalk.dao.TdNtSearchHisoryDao;
import com.nativetalk.dao.TdNtTeacherDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/8/23.
 */
@Service
public class ThreeIndexServiceImpl implements ThreeIndexService {
    private static Logger logger = Logger.getLogger(ThreeIndexServiceImpl.class);
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Resource
    private TdNtLiveCourseHisDao tdNtLiveCourseHisDao;
    @Resource
    private TdNtSearchHisoryDao tdNtSearchHisoryDao;

    @Override
    public RetInfo index(TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":index:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap =
                    (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);

            Map<String , Object> map = new HashMap<>();
            List<TdNtBannerInfoIndex> tdNtBannerInfoIndices = (List<TdNtBannerInfoIndex>) MemcachedUtils.get(MemcachedKey.bannerInfoAPPIndexs);

            map.put("banner",tdNtBannerInfoIndices);
            map.put("livies" , tdNtLiveCourseHisDao.findTdNtLiveCourseHisIndex());
            map.put("teacherRounds" , tdNtTeacherDao.findTdNtTdNtTeacherIndexRound());

            Page page = new Page();
            page.setCurrentPage(0);
            page.setPageSize(9);
            page.setObj(0);
            List<TdNtTeacherList> tdNtTeacherLists = tdNtTeacherDao.findTdNtTeacherList(page);
            for (int i = 0; i < tdNtTeacherLists.size(); i++) {
                if (tdNtTeacherLists.get(i).getDict_id() != null) {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherLists.get(i).getDict_id());
                    tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                } else {
                    TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                    tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                }
                String country = tdNtTeacherLists.get(i).getNationality();
                if (country.equals("0") || country.equals("315")) {
                    country = "1";
                }
                String nationality = tsNtDictDeatilsMap.get(country).getCode_name();
                tdNtTeacherLists.get(i).setNationality(nationality);
                /**
                 * 查找老师粉丝数
                 */
                int funsnumber = tdNtTeacherDao.findFunsSize(tdNtTeacherLists.get(i).getTea_id());
                tdNtTeacherLists.get(i).setFans_size(funsnumber);
                /**
                 * 查找是否收藏老师 1未收藏0已收藏
                 */
                String collection_flag = "1";
                //判断是否登录，没有登录默认为未收藏
                if (tdNtMember != null) {
                    Map<String, Object> obj = new HashMap<String, Object>();
                    obj.put("tea_id", tdNtTeacherLists.get(i).getTea_id());
                    obj.put("memb_id", tdNtMember.getMemb_id());
                    String flag = tdNtTeacherDao.foundTdNtTeacherCollection(obj);
                    if (flag != null) {
                        collection_flag = flag;
                    }
                }
                tdNtTeacherLists.get(i).setCollection_flag(collection_flag);
            }
            map.put("teachers", tdNtTeacherLists);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo teacherList(Page page,TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":teacherList:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap =
                    (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);

            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtTeacherDao.findTdNtTeacherListSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);
                List<TdNtTeacherList> tdNtTeacherLists = tdNtTeacherDao.findTdNtTeacherList(page);
                for (int i = 0; i < tdNtTeacherLists.size(); i++) {
                    if (tdNtTeacherLists.get(i).getDict_id() != null) {
                        TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherLists.get(i).getDict_id());
                        tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                    } else {
                        TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                        tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                    }
                    String country = tdNtTeacherLists.get(i).getNationality();
                    if (country.equals("0") || country.equals("315")) {
                        country = "1";
                    }
                    String nationality = tsNtDictDeatilsMap.get(country).getCode_name();
                    tdNtTeacherLists.get(i).setNationality(nationality);
                    /**
                     * 查找老师粉丝数
                     */
                    int funsnumber = tdNtTeacherDao.findFunsSize(tdNtTeacherLists.get(i).getTea_id());
                    tdNtTeacherLists.get(i).setFans_size(funsnumber);
                    /**
                     * 查找是否收藏老师 1未收藏0已收藏
                     */
                    String collection_flag = "1";
                    //判断是否登录，没有登录默认为未收藏
                    if (tdNtMember != null) {
                        Map<String, Object> obj = new HashMap<String, Object>();
                        obj.put("tea_id", tdNtTeacherLists.get(i).getTea_id());
                        obj.put("memb_id", tdNtMember.getMemb_id());
                        String flag = tdNtTeacherDao.foundTdNtTeacherCollection(obj);
                        if (flag != null) {
                            collection_flag = flag;
                        }
                    }
                    tdNtTeacherLists.get(i).setCollection_flag(collection_flag);
                }
                result.put("teachers", tdNtTeacherLists);
                result.put("totalPage", totalPage);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取列表成功");
                retInfo.setObj(result);

            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("暂无老师");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo searchTeacherList(Page page,TdNtMember tdNtMember) {
        String logInfo = this.getClass().getName() + ":searchTeacherList:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, TsNtDictDeatil> tsNtDictDeatilsMap =
                    (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);

            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtTeacherDao.searchTdNtTeacherListSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                if (currentPage == 1 && tdNtMember != null) {
                    TdNtSearchHistory tdNtSearchHistory =  new TdNtSearchHistory();
                    tdNtSearchHistory.setMemb_id(tdNtMember.getMemb_id());
                    tdNtSearchHistory.setSea_content(page.getObj().toString());
                    tdNtSearchHistory.setSea_his_month(CommonUtil.getMonth(new Date()));
                    tdNtSearchHisoryDao.insertTdNtSearchHistory(tdNtSearchHistory);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);
                List<TdNtTeacherList> tdNtTeacherLists = tdNtTeacherDao.searchTdNtTeacherList(page);
                for (int i = 0; i < tdNtTeacherLists.size(); i++) {
                    if (tdNtTeacherLists.get(i).getDict_id() != null) {
                        TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get(tdNtTeacherLists.get(i).getDict_id());
                        tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                    } else {
                        TsNtDictDeatil tsNtDictDeatil = tsNtDictDeatilsMap.get("3");
                        tdNtTeacherLists.get(i).setDict_id(tsNtDictDeatil.getCode_name());
                    }
                    String country = tdNtTeacherLists.get(i).getNationality();
                    if (country.equals("0") || country.equals("315")) {
                        country = "1";
                    }
                    String nationality = tsNtDictDeatilsMap.get(country).getCode_name();
                    tdNtTeacherLists.get(i).setNationality(nationality);
                    /**
                     * 查找老师粉丝数
                     */
                    int funsnumber = tdNtTeacherDao.findFunsSize(tdNtTeacherLists.get(i).getTea_id());
                    tdNtTeacherLists.get(i).setFans_size(funsnumber);
                    /**
                     * 查找是否收藏老师 1未收藏0已收藏
                     */
                    String collection_flag = "1";
                    //判断是否登录，没有登录默认为未收藏
                    if (tdNtMember != null) {
                        Map<String, Object> obj = new HashMap<String, Object>();
                        obj.put("tea_id", tdNtTeacherLists.get(i).getTea_id());
                        obj.put("memb_id", tdNtMember.getMemb_id());
                        String flag = tdNtTeacherDao.foundTdNtTeacherCollection(obj);
                        if (flag != null) {
                            collection_flag = flag;
                        }
                    }
                    tdNtTeacherLists.get(i).setCollection_flag(collection_flag);
                }
                result.put("teachers", tdNtTeacherLists);
                result.put("totalPage", totalPage);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取列表成功");
                retInfo.setObj(result);

            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("暂无老师");
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
